﻿using System.Collections.ObjectModel;
using YKXiangQi.Common;
using YKXiangQi.Enums;
using YKXiangQi.Models;
using YKXiangQi.Utilities;

namespace YKXiangQi.ViewModels
{
    public class MoveDetailsViewModel: DialogViewModel
    {
        public BoardViewModel Board { get; private set; }

        public MoveList MoveList { get; set; }

        public MoveDetailsViewModel(MoveList moves)
        {
            MoveList = moves;
            Board = new BoardViewModel(BoardSize.Small, MoveList.StartFen, BoardMode.ReadOnly);
            InitMoveDetailsList();
        }

        private ObservableCollection<MoveDetails> _moveDetailsList; 
        public ObservableCollection<MoveDetails> MoveDetailsList
        {
            get { return _moveDetailsList; }
            set { _moveDetailsList = value;
                RaisePropertyChanged(() => MoveDetailsList);
            }
        }

        private MoveDetails _selectedItem;
        public MoveDetails SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                Board.SetBoard(_selectedItem.Fen);
                RaisePropertyChanged(() => SelectedItem);
            }
        }

        private void InitMoveDetailsList()
        {
            _moveDetailsList = new ObservableCollection<MoveDetails>(MoveList.GenerateMoveDetailsList());
           
            var startMove = new MoveDetails() {Fen = MoveList.StartFen, Notation = "----"};
            _moveDetailsList.Insert(0, startMove);

            RaisePropertyChanged(() => MoveDetailsList);
            SelectedItem = startMove;
        }
    }
}
